package suite.cases
{
	import com.firemoss.magicbus.rpc.Service;
	import com.firemoss.magicbus.rpc.ServiceMethod;
	
	import flexunit.framework.Assert;
	
	import mx.rpc.AbstractOperation;
	import mx.rpc.AsyncToken;
	import mx.rpc.Responder;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.flexunit.async.Async;
	
	import util.SimpleLog;
	
	public class ServiceTests
	{
		/*
		[Test]
		public function declared_service_exists() : void
		{
			var service : Service = DeclaredComponents.instance.testService
			
			Assert.assertTrue( service != null )
		}
		
		[Test]
		public function get_undeclared_operation_returns_ServiceMethod() : void
		{
			var service : Service = DeclaredComponents.instance.testService
			var operation : AbstractOperation = service.undeclaredMethod
			
			Assert.assertTrue( operation is ServiceMethod )
		}
		
		[Test]
		public function get_declared_operation_returns_declared_ServiceMethod() : void
		{
			var service : Service = DeclaredComponents.instance.testService
			var operation : AbstractOperation = service.declaredMethod
			
			Assert.assertTrue( operation == DeclaredComponents.instance.declaredMethod )
		}
		
		[Test(async)]
		public function mock_operation_returns_declared_mock_result() : void
		{
			var service : Service = DeclaredComponents.instance.testService

			AsyncToken( service.declaredMockMethod() ).addResponder( new Responder (
				Async.asyncHandler(this, mock_operation_returns_declared_mock_result_response, 1001),
				asyncFault
			));
		}
		
		private function mock_operation_returns_declared_mock_result_response( event : ResultEvent, data : Object ) : void
		{
			Assert.assertTrue( event.result == DeclaredComponents.instance.declaredMockMethod.returnValue )
		}

		*/
		[Test(async)]
		public function all_chains_executed_with_global_first() : void
		{
			SimpleLog.clear()
			
			var service : Service = DeclaredComponents.instance.testService

			AsyncToken( service.filteredMockMethod() ).addResponder( new Responder (
				Async.asyncHandler(this, all_chains_executed_with_global_first_response, 1001),
				asyncFault
			));
			
		}

		private function all_chains_executed_with_global_first_response( event : ResultEvent, data : Object ) : void
		{
			var log : Array = SimpleLog.log
			
			Assert.assertTrue( SimpleLog.log.length == 6 )
			Assert.assertTrue( SimpleLog.log[ 0 ] == "beforeFilterGlobal" )
			Assert.assertTrue( SimpleLog.log[ 1 ] == "beforeFilterLocal" )
			Assert.assertTrue( SimpleLog.log[ 2 ] == "responseFilterLocal" )
			Assert.assertTrue( SimpleLog.log[ 3 ] == "responseFilterGlobal" )
			Assert.assertTrue( SimpleLog.log[ 4 ] == "resultFilterLocal" )
			Assert.assertTrue( SimpleLog.log[ 5 ] == "resultFilterGlobal" )
			Assert.assertTrue( event.result == "FILTERED RESULT" )
			Assert.assertTrue( event.token.result == "FILTERED RESULT" )
		}
		
		private function asyncFault( event : FaultEvent ) : void
		{
			Assert.fail( "Async fault" )
		}
	}
}